
import com.winstar.entity.Apple;
import com.winstar.vo.Dish;
import module.predicate.Predicate;
import module.predicate.ThiFunction;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import static java.util.Comparator.comparing;

import static java.util.stream.Collectors.toList;

/**
 * Created by Administrator on 2017/6/19.
 */
public class Lambda {
    public static void main(String[] args){
        filter2();

    }

    public static void number(){
        List<Dish> menu = Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH) );
    }



    public static void function3(){
        BiFunction<String, Double, Apple > c = Apple::new;
        Apple a = c.apply("yellow",0.5);
        System.out.println(a);
    }

    public static void function2(){
        Function<Double,Apple> c = Apple::new;
        Apple a = c.apply(0.5);
        System.out.println(a);
    }

    public static void function1(){
        Function<Double,Apple> c = (weight) -> new Apple("black",weight,weight);
        Apple a = c.apply(0.5);
        System.out.println(a);
    }

    public static void filter(){
        List<Apple> inventory = new ArrayList();
        ThiFunction<String, Double, Double, Apple > appleFactory = Apple::new;
        appleFactory.apply("red",0.1,2.0);
        Apple a1 = appleFactory.apply("red",0.1,2.0);
        Apple a2 = appleFactory.apply("green",0.2,3.0);
        Apple a3 = appleFactory.apply("yellow",0.3,1.0);
        inventory.add(a1);
        inventory.add(a2);
        inventory.add(a3);
        //Arrays.sort(words);
        //words.forEach(x -> System.out.println(x));
        List<Apple> expensiveApples = inventory.stream().filter( (Apple a) -> a.getPrice() > 2 ).collect(toList());
        System.out.println("expensiveApples→" + expensiveApples);
        List<Apple> heavyApples = inventory.stream().filter( (Apple a) -> a.getWeight() >0.15 ).collect(toList());
        System.out.println("heavyApples→" + heavyApples);
    }

    public static void filter2(){
        List<Apple> inventory = new ArrayList();
        Apple a1 = new Apple("red",0.1,2.0);
        Apple a2 = new Apple("green",0.2,3.0);
        Apple a3 = new Apple("yellow",0.3,1.0);
        Apple a4 = new Apple("blue",0.4,2.0);
        Apple a5 = new Apple("black",0.2,2.0);
        Apple a6 = new Apple("white",0.3,2.0);
        inventory.add(a1);
        inventory.add(a2);
        inventory.add(a3);
        inventory.add(a4);
        inventory.add(a5);
        inventory.add(a6);

        //对inventory进行排序，先按重量倒叙，重量相同的按价格排序
        inventory.sort(comparing(Apple::getWeight).reversed().thenComparing(Apple::getPrice));
        List<String> apples = inventory.stream().filter(apple -> apple.getWeight() > 0.2).map(Apple::getColor).collect(toList());
        System.out.println(apples);

    }

    public static <T> List<T> filter(List<T> list, Predicate<T> p){
        List<T> result = new ArrayList<>();
        for(T e: list){
            if(p.test(e)){
                result.add(e);
            }
        }
        return result;
    }
}
